﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.CUiRenameToolFilter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.RenameTool;

public class CUiRenameToolFilter : TxWindow, IComponentConnector
{
  private ObservableCollection<CApRenameElement> objectTypes = new ObservableCollection<CApRenameElement>();
  private bool windowIsClosing;
  public bool DialogResult;
  internal ListView listViewTypes;
  internal CheckBox checkBoxSelectAll;
  private bool _contentLoaded;

  public ObservableCollection<CApRenameElement> ObjectTypes
  {
    get => this.objectTypes;
    set => this.objectTypes = value;
  }

  public List<string> GetFilteredOutObjects()
  {
    List<string> filteredOutObjects = new List<string>();
    foreach (CApRenameElement objectType in (Collection<CApRenameElement>) this.ObjectTypes)
    {
      if (!objectType.IsChecked)
        filteredOutObjects.Add(objectType.ObjectType);
    }
    return filteredOutObjects;
  }

  public CUiRenameToolFilter(List<CApRenameElement> types)
  {
    this.InitializeComponent();
    this.windowIsClosing = false;
    foreach (CApRenameElement capRenameElement in new SortedSet<CApRenameElement>((IEnumerable<CApRenameElement>) types))
    {
      capRenameElement.Checked += new CheckStatusDelegate(this.item_Checked);
      this.ObjectTypes.Add(capRenameElement);
    }
    this.listViewTypes.ItemsSource = (IEnumerable) this.ObjectTypes;
    this.SetSelectAllCheckedState();
  }

  private void item_Checked() => this.SetSelectAllCheckedState();

  private void SetSelectAllCheckedState()
  {
    this.checkBoxSelectAll.Checked -= new RoutedEventHandler(this.checkBoxSelectAll_Checked);
    this.checkBoxSelectAll.Unchecked -= new RoutedEventHandler(this.checkBoxSelectAll_Unchecked);
    bool flag1 = false;
    bool flag2 = false;
    foreach (CApRenameElement objectType in (Collection<CApRenameElement>) this.ObjectTypes)
    {
      if (objectType.IsChecked)
        flag1 = true;
      else
        flag2 = true;
    }
    if (flag1 & flag2)
      this.checkBoxSelectAll.IsChecked = new bool?();
    else if (flag1)
      this.checkBoxSelectAll.IsChecked = new bool?(true);
    else
      this.checkBoxSelectAll.IsChecked = new bool?(false);
    this.checkBoxSelectAll.Checked += new RoutedEventHandler(this.checkBoxSelectAll_Checked);
    this.checkBoxSelectAll.Unchecked += new RoutedEventHandler(this.checkBoxSelectAll_Unchecked);
  }

  private void listViewTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (this.listViewTypes.SelectedIndex == -1)
      return;
    if (this.listViewTypes.SelectedItem is CApRenameElement selectedItem)
      selectedItem.IsChecked = !selectedItem.IsChecked;
    this.listViewTypes.SelectedIndex = -1;
  }

  private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
  {
    if (!this.checkBoxSelectAll.IsChecked.HasValue)
    {
      this.checkBoxSelectAll.IsChecked = new bool?(false);
    }
    else
    {
      CheckBox checkBoxSelectAll = this.checkBoxSelectAll;
      bool? isChecked = this.checkBoxSelectAll.IsChecked;
      bool? nullable = isChecked.HasValue ? new bool?(!isChecked.GetValueOrDefault()) : new bool?();
      checkBoxSelectAll.IsChecked = nullable;
    }
  }

  private void checkBoxSelectAll_Click(object sender, RoutedEventArgs e)
  {
    if (this.checkBoxSelectAll.IsChecked.HasValue)
      return;
    this.checkBoxSelectAll.IsChecked = new bool?(false);
  }

  private void checkBoxSelectAll_Checked(object sender, RoutedEventArgs e)
  {
    foreach (CApRenameElement objectType in (Collection<CApRenameElement>) this.ObjectTypes)
      objectType.IsChecked = true;
  }

  private void checkBoxSelectAll_Unchecked(object sender, RoutedEventArgs e)
  {
    foreach (CApRenameElement objectType in (Collection<CApRenameElement>) this.ObjectTypes)
      objectType.IsChecked = false;
  }

  private void Button_Click_OK(object sender, RoutedEventArgs e)
  {
    this.DialogResult = true;
    this.CustomClose();
  }

  private void Button_Click_Cancel(object sender, RoutedEventArgs e) => this.CustomClose();

  private void TxWindow_Deactivated(object sender, EventArgs e) => this.CustomClose();

  private void CustomClose()
  {
    if (this.windowIsClosing)
      return;
    this.windowIsClosing = true;
    ((Window) this).Close();
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/renametool/cuirenametoolfilter.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((Window) target).Deactivated += new EventHandler(this.TxWindow_Deactivated);
        break;
      case 2:
        this.listViewTypes = (ListView) target;
        this.listViewTypes.SelectionChanged += new SelectionChangedEventHandler(this.listViewTypes_SelectionChanged);
        break;
      case 3:
        this.checkBoxSelectAll = (CheckBox) target;
        this.checkBoxSelectAll.Click += new RoutedEventHandler(this.checkBoxSelectAll_Click);
        this.checkBoxSelectAll.Checked += new RoutedEventHandler(this.checkBoxSelectAll_Checked);
        this.checkBoxSelectAll.Unchecked += new RoutedEventHandler(this.checkBoxSelectAll_Unchecked);
        break;
      case 4:
        ((UIElement) target).MouseUp += new MouseButtonEventHandler(this.TextBlock_MouseUp);
        break;
      case 5:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.Button_Click_Cancel);
        break;
      case 6:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.Button_Click_OK);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
